﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using VisualWip.Core;
using VisualWip.Core.Columns;
using VisualWip.Core.Interfaces;
using VisualWip.PresentationModel;
using VisualWip.PresentationModel.WorkItem;
using VisualWip.Util;

namespace UtilTests
{
    [TestClass]
    public class ViewModelFactoryTests
    {
        private UnityContainer _container;
        private MockRepository _repository;
        private IColumnBuilder _columnBuilder;
        private ViewModelFactory _sut;

        public TestContext TestContext { get; set; }

        #region Additional test attributes

        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //

        #endregion

        [TestInitialize]
        public void MyTestInitialize()
        {
            _container = new UnityContainer();
            _sut = new ViewModelFactory(_container);
            _repository = new MockRepository();
            _columnBuilder = _repository.Stub<IColumnBuilder>();
            _container.RegisterInstance(new Application());
            _container.RegisterInstance(_columnBuilder);
        }

        [TestMethod]
        public void CreateViewModel_GivenAMatchingRegistration_ShouldReturn_CorrectViewModel()
        {
            _container.RegisterType<WorkColumnViewModel>();

            _sut.RegisterViewModel<WorkColumnBase, WorkColumnViewModel>();

            var column = new WorkSingleColumn();

            var result = _sut.CreateViewModel<WorkColumnViewModel>(column);

            Assert.IsInstanceOfType(result, typeof (WorkColumnViewModel));
        }

        [TestMethod, ExpectedException(typeof (NotImplementedException))]
        public void CreateViewModel_GivenNonMatchingRegistration_ShouldThrowException()
        {
            _container.RegisterType<WorkColumnViewModel>();

            _sut.RegisterViewModel<WorkColumnBase, WorkColumnViewModel>();

            var column = new RichTextColumn();

            var result = _sut.CreateViewModel<WorkColumnViewModel>(column);
        }
    }
}